﻿using System;
using System.Windows.Media;
using MoveTheCar.Utilities;
using System.Windows.Media.Imaging;
using System.Reflection;
using System.IO;

namespace MoveTheCar.Model
{
    public class Car : INPC
    {
        #region Members
        private double x;
        private double y;
        private bool isUser;
        private Brush fill;
        private State orientation;
        private double height;
        private double width;
        #endregion Members

        #region Constructor
        public Car(bool isUser, State orientation, double baseLength)
        {
            Random rand = new Random((int)DateTime.Now.Ticks);
            X = rand.Next(1, 750);
            Y = rand.Next(1, 750);

            this.isUser = isUser;
            Orientation = orientation;

            ImageBrush imgBrush = new ImageBrush();
            if (Orientation == State.Horizontal)
            {
                Height = baseLength;
                Width = baseLength * 2;

                if (isUser)
                    imgBrush.ImageSource = GoalCarHorizontalImage;
                else
                    imgBrush.ImageSource = ObstacleCarHorizontalImage;
            }
            else
            {
                Height = baseLength * 2;
                Width = baseLength;

                if (isUser)
                    imgBrush.ImageSource = GoalCarVerticalImage;
                else
                    imgBrush.ImageSource = ObstacleCarVerticalImage;
            }
            Fill = imgBrush;
        }

        public Car(bool isUser, State orientation, double baseLength, double x, double y)
            : this(isUser, orientation, baseLength)
        {
            X = x;
            Y = y;
        }
        #endregion Constructor

        #region Databinded Methods
        public double X
        {
            get { return x; }
            set
            {
                if (x == value)
                    return;

                x = value;
                RaisePropertyChanged("X");
            }
        }
        public double Y
        {
            get { return y; }
            set
            {
                if (y == value)
                    return;

                y = value;
                RaisePropertyChanged("Y");
            }
        }
        public bool IsUser
        {
            get { return isUser; }
        }
        public Brush Fill
        {
            get { return fill; }
            set
            {
                if (fill == value)
                    return;

                fill = value;
                RaisePropertyChanged("Fill");
            }
        }
        public State Orientation
        {
            get { return orientation; }
            set
            {
                if (orientation == value)
                    return;

                orientation = value;
                RaisePropertyChanged("Orientation");
            }
        }
        public double Height
        {
            get { return height; }
            set
            {
                if (height == value)
                    return;

                height = value;
                RaisePropertyChanged("Height");
            }
        }
        public double Width
        {
            get { return width; }
            set
            {
                if (width == value)
                    return;

                width = value;
                RaisePropertyChanged("Width");
            }
        }

        #region BitmapImages
        public BitmapImage GoalCarHorizontalImage
        {
            get {
                Assembly _assembly;
                _assembly = Assembly.GetExecutingAssembly();
                Stream imageStream = _assembly.GetManifestResourceStream("MoveTheCar.Resources.goalcar-h.png");

                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = imageStream;
                bitmap.EndInit();

                return bitmap;
            }
        }
        public BitmapImage GoalCarVerticalImage
        {
            get
            {
                Assembly _assembly;
                _assembly = Assembly.GetExecutingAssembly();
                Stream imageStream = _assembly.GetManifestResourceStream("MoveTheCar.Resources.goalcar-v.png");

                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = imageStream;
                bitmap.EndInit();

                return bitmap;
            }
        }
        public BitmapImage ObstacleCarHorizontalImage
        {
            get {
                Assembly _assembly;
                _assembly = Assembly.GetExecutingAssembly();
                Stream imageStream = _assembly.GetManifestResourceStream("MoveTheCar.Resources.obstaclecar-h.png");

                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = imageStream;
                bitmap.EndInit();

                return bitmap;
            }
        }
        public BitmapImage ObstacleCarVerticalImage
        {
            get {
                Assembly _assembly;
                _assembly = Assembly.GetExecutingAssembly();
                Stream imageStream = _assembly.GetManifestResourceStream("MoveTheCar.Resources.obstaclecar-v.png");

                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = imageStream;
                bitmap.EndInit();

                return bitmap;
            }
        }
        #endregion BitmapImages
        #endregion Databinded Methods

        #region Methods
        public void Resize(double baseLength)
        {
            if (Orientation == State.Horizontal)
            {
                Height = baseLength;
                Width = baseLength * 2;
            }
            else
            {
                Height = baseLength * 2;
                Width = baseLength;
            }
        }
        #endregion Methods

        #region Events
        #endregion Events
    }
}